package test.general_data_handle;

import test.pojo.Person;


import java.io.*;
import java.util.List;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2022/8/3 9:50
 * @ Description:
 */
public class GeneralReadFromDbAndWriteToFile {

    public static boolean generalReadFromDbAndWriteToFile(List<Person> list) {
        File file = new File("D:\\资料\\sysLogV2.txt");
        try(FileOutputStream fileOutputStream = new FileOutputStream(file , true);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
            BufferedWriter br = new BufferedWriter(outputStreamWriter);){
            list.stream().forEach(new Consumer<Person>() {
                @Override
                public void accept(Person person) {
                    String personToString = person.toString();
                    try {
                        br.write(personToString);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        br.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

        }catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
}
